<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <!-- 点击首页会跳转到home -->
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片试图区 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            class="input-with-select"
            clearable
            @clear="getGoodsList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <el-table :data="goodsList" border style="width: 100%">
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template v-slot="scope">{{scope.row.add_time}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <!-- <el-button type="primary" icon="el-icon-edit" size="mini" @click="editGoods">编辑</el-button> -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row.goods_id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 40]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 编辑对话框 -->
    <!-- <el-dialog title="编辑商品" :visible.sync="dialogVisibleEdit" width="50%">
      <el-form :model="editForm" :rules="rulesEdit" ref="editFormRef" label-width="100px">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleEdit = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </span>
    </el-dialog>-->
  </div>
</template>

<script>
export default {
  data() {
    return {
      //商品数据
      goodsList: [],
      //查询参数对象
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5
      },
      //数据总条数
      total: 0,
      //控制编辑窗口的显示
      dialogVisibleEdit: false,
      //编辑表单对象
      editForm: {},
      //编辑商品表单验证规则
      rulesEdit: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" }
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" }
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    //获得商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200) {
        return;
      } else {
        // this.$message.success("获取商品列表成功");
        // console.log(res.data);

        this.goodsList = res.data.goods;
        this.total = res.data.total;
      }
    },
    //监听pageSize
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsList();
    },
    //监听pagenum
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum;
      this.getGoodsList();
    },
    //删除
    async deleteGoods(id) {
      const result = await this.$confirm(
        "此操作将永久删除该商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(error => error);
      if (result !== "confirm") {
        return;
      } else {
        const { data: res } = await this.$http.delete("goods/" + id);
        if (res.meta.status !== 200) {
          return this.$message.error("删除失败");
        } else {
          this.$message.success("删除商品成功");
          this.getGoodsList();
        }
      }
    },
    //编辑商品显示
    // editGoods() {
    // const { data: res } = await this.$http.get("goods/" + id);
    // if (res.meta.status !== 201) {
    //   return this.$message.error("获取失败");
    // } else {
    //   this.editForm = res.data;
    // }
    //   this.dialogVisibleEdit = true;
    // },
    //点击确认编辑商品
    // edit(){
    //   this.$refs.editFormRef.validate(async valid=>{
    //     const{data:res}=await this.$http.put('goods/:id')
    //   })
    //   dialogVisibleEdit = false
    // },

    //添加商品
    addGoods() {
      this.$router.push("/goods/add");
    }
  }
};
</script>

<style lang="less" scoped>
</style>